package com.ruoyi.web.controller.pull;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ERP.MaterialDataErp;
import com.ruoyi.system.domain.Pull.MaterialFeed;
import com.ruoyi.system.domain.SetModel;
import com.ruoyi.system.domain.base.BaseMaterial;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.mapper.BaseShopMapper;
import com.ruoyi.system.mapper.MaterialDataErpMapper;
import com.ruoyi.system.mapper.SetModelMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMaterialFeedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料补料设置Controller
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Api(value = "MaterialFeedController", tags = "工位物料补料接口api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pull/feed" )
public class MaterialFeedController extends BaseController {

    private final IMaterialFeedService iMaterialFeedService;

    @Autowired
    private MaterialDataErpMapper materialDataErpMapper;
    @Autowired
    private SetModelMapper setModelMapper;
    @Autowired
    private BaseShopMapper baseShopMapper;

    /**
     * 查询物料补料设置列表
     */
    @ApiOperation(value = "查询物料补料设置列表",response = MaterialFeed.class)
    @GetMapping("/list")
    public TableDataInfo list(MaterialFeed materialFeed)
    {
        startPage();
        LambdaQueryWrapper<MaterialFeed> lqw = new LambdaQueryWrapper<MaterialFeed>();
        if (StringUtils.isNotBlank(materialFeed.getMaterialCode())){
            lqw.eq(MaterialFeed::getMaterialCode ,materialFeed.getMaterialCode());
        }
        if (StringUtils.isNotBlank(materialFeed.getLineName())){
            lqw.like(MaterialFeed::getLineName ,materialFeed.getLineName());
        }
        if (StringUtils.isNotBlank(materialFeed.getStationName())){
            lqw.like(MaterialFeed::getStationName ,materialFeed.getStationName());
        }
        if (StringUtils.isNotBlank(materialFeed.getProductCode())){
            lqw.eq(MaterialFeed::getProductCode ,materialFeed.getProductCode());
        }
        List<MaterialFeed> list = iMaterialFeedService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出物料补料设置列表
     */
    @Log(title = "物料补料设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MaterialFeed materialFeed) {
        LambdaQueryWrapper<MaterialFeed> lqw = new LambdaQueryWrapper<MaterialFeed>(materialFeed);
        List<MaterialFeed> list = iMaterialFeedService.list(lqw);
        ExcelUtil<MaterialFeed> util = new ExcelUtil<MaterialFeed>(MaterialFeed. class);
        return util.exportExcel(list, "feed" );
    }

    /**
     * 获取物料补料设置详细信息
     */
    @ApiOperation(value = "获取物料补料设置详细信息")
    @GetMapping(value = "/{feedId}" )
    public AjaxResult getInfo(@PathVariable("feedId" ) Long feedId) {
        return AjaxResult.success(iMaterialFeedService.getById(feedId));
    }




    /**
     * 新增物料补料设置
     */
    @ApiOperation(value = "新增物料补料设置")
    @Log(title = "物料补料设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialFeed materialFeed) {
        materialFeed.setCreateBy(SecurityUtils.getUsername());
        materialFeed.setCreateTime(new Date());

        //根据产品编码获取产品名称
        SetModel setModel = setModelMapper.selectOne(new QueryWrapper<SetModel>().eq("model_des_local_code", materialFeed.getProductCode()));
        if(ObjectUtil.isNotNull(setModel)){
            materialFeed.setProductName(setModel.getLedName());
        }
        //根据物料编码获取物料名称
        MaterialDataErp ma = materialDataErpMapper.selectOne(new QueryWrapper<MaterialDataErp>().eq("data_id", materialFeed.getDataId()));
        if(ObjectUtil.isNotNull(ma)){
            materialFeed.setMaterialCode(ma.getInvCode());
            materialFeed.setMaterialName(ma.getInvName());
        }

        //根据工位获取产线编码与名称
        BaseShop shop = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_code", materialFeed.getStationCode()));
        if(ObjectUtil.isNotNull(shop)){
            BaseShop shop1 = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_id", shop.getParentId()));
            if(ObjectUtil.isNotNull(shop1)){
                materialFeed.setLineCode(shop1.getShopCode());
                materialFeed.setLineName(shop1.getShopName());
            }
        }
        return toAjax(iMaterialFeedService.save(materialFeed) ? 1 : 0);
    }

    /**
     * 修改物料补料设置
     */
    @ApiOperation(value = "修改物料补料设置")
    @Log(title = "物料补料设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialFeed materialFeed) {
        materialFeed.setUpdateBy(SecurityUtils.getUsername());
        materialFeed.setUpdateTime(new Date());

        //根据产品编码获取产品名称
        SetModel setModel = setModelMapper.selectOne(new QueryWrapper<SetModel>().eq("model_des_local_code", materialFeed.getProductCode()));
        if(ObjectUtil.isNotNull(setModel)){
            materialFeed.setProductCode(setModel.getModelCode());
            materialFeed.setProductName(setModel.getModel());
        }
        //根据物料编码获取物料名称
        MaterialDataErp ma = materialDataErpMapper.selectOne(new QueryWrapper<MaterialDataErp>().eq("inv_code", materialFeed.getMaterialCode()));
        if(ObjectUtil.isNotNull(ma)){
            materialFeed.setMaterialCode(ma.getInvCode());
            materialFeed.setMaterialName(ma.getInvName());
        }

        //根据工位获取产线编码与名称
        BaseShop shop = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_code", materialFeed.getStationCode()));
        if(ObjectUtil.isNotNull(shop)){
            BaseShop shop1 = baseShopMapper.selectOne(new QueryWrapper<BaseShop>().eq("shop_id", shop.getParentId()));
            if(ObjectUtil.isNotNull(shop1)){
                materialFeed.setLineCode(shop1.getShopCode());
                materialFeed.setLineName(shop1.getShopName());
            }
        }

        return toAjax(iMaterialFeedService.updateById(materialFeed) ? 1 : 0);
    }

    /**
     * 删除物料补料设置
     */
    @ApiOperation(value = "删除物料补料设置")
    @Log(title = "物料补料设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{feedIds}" )
    public AjaxResult remove(@PathVariable Long[] feedIds) {
        return toAjax(iMaterialFeedService.removeByIds(Arrays.asList(feedIds)) ? 1 : 0);
    }
}
